package com.vinoxm.common.utils;

import java.util.List;
import java.util.Set;

public class CollectionUtils {

    public static <T extends List<E>, E> boolean some(T collection, Calculator<? super E> calculator) {
        if (null != collection) {
            for (E o : collection) {
                if (calculator.doSomething(o)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static <T extends Set<E>, E> boolean some(T collection, Calculator<? super E> calculator) {
        if (null != collection) {
            for (E o : collection) {
                if (calculator.doSomething(o)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static <T extends List<E>, E> E find(T collection, Calculator<? super E> calculator) {
        if (null != collection) {
            for (E o : collection) {
                if (calculator.doSomething(o)) {
                    return o;
                }
            }
        }
        return null;
    }

    public interface Calculator<E> {
        boolean doSomething(E item);
    }
}